/*
 * status.c
 *
 *  Created on: Mar 31, 2012
 *      Author: lin
 */

#include <stddef.h>
#include <string.h>
#include <assert.h>

#include "status.h"

const status s_ok = { cOK, { '\0', } };

void status_init(status *s, status_code code, const char *msg1,
        const char *msg2) {
    assert(code != cOK);
    s->code = code;
    int len1 = 0, len2 = 0;
    int remain = MAX_STATUS_MSG_LEN - 1;
    int cpy, end = 0;
    if (msg1) {
        len1 = strlen(msg1);
        if (len1) {
            cpy = remain > len1 ? len1 : remain;
            memcpy(s->msg, msg1, cpy);
            remain -= cpy;
            end += cpy;
        }

    }
    if (msg2) {
        len2 = strlen(msg2);
        if (len2 && remain-- > 0) {
            s->msg[len1] = ':';
            cpy = remain > len2 ? len2 : remain;
            memcpy(s->msg + len1 + 1, msg1, cpy);
            end += cpy;
            ++end;
        }
    }
    s->msg[end] = '\0';
}
